package com.stream.service.impl;

import com.google.common.collect.Maps;
import com.stream.model.entity.SysUserEntity;
import com.stream.repository.SysUserRepository;
import com.stream.service.SySUserRoleService;
import com.stream.service.SysRoleService;
import com.stream.service.SysUserService;
import com.stream.utils.Constant;
import com.stream.utils.RRException;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 * Created by yebaokang on 2017/5/23.
 */

@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService{


    @SuppressWarnings({"all"})
    @Autowired
    private SysUserRepository sysUserRepository;

    @SuppressWarnings("all")
    @Autowired
    private SySUserRoleService sysUserRoleService;

    @SuppressWarnings("all")
    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public List<String> queryAllPerms(Long userId) {
        return sysUserRepository.queryAllPerms(userId);
    }

    @Override
    public List<Long> queryAllMenuId(Long userId) {
        return sysUserRepository.queryAllMenuId(userId);
    }

    @Override
    public SysUserEntity queryByUserName(String userName) {
        return sysUserRepository.queryByUserName(userName);
    }

    @Override
    public SysUserEntity queryObject(Long userId) {
        return sysUserRepository.queryObject(userId);
    }

    @Override
    public List<SysUserEntity> queryList(Map<String, Object> map) {
        return sysUserRepository.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return sysUserRepository.queryTotal(map);
    }

    @Override
    public void save(SysUserEntity user) {
        user.setCreateTime(new Date());
        user.setPassword(new Sha256Hash(user.getPassword()).toHex());
        sysUserRepository.save(user);

        //检查角色是否越权
        checkRole(user);

        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
    }

    @Override
    @Transactional
    public void update(SysUserEntity user) {
        if(StringUtils.isBlank(user.getPassword())){
            user.setPassword(null);
        }else{
            user.setPassword(new Sha256Hash(user.getPassword()).toHex());
        }
        sysUserRepository.update(user);

        //检查角色是否越权
        checkRole(user);

        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
    }

    @Override
    public void deleteBatch(Long[] userIds) {
        sysUserRepository.deleteBatch(userIds);
    }

    @Override
    public int updatePassword(Long userId, String password, String newPassWord) {
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("password", password);
        map.put("newPassword", newPassWord);
        return sysUserRepository.updatePassWord(map);
    }

    /**
     * 检查角色是否越权
     *
     * @param user
     */
    private void checkRole(SysUserEntity user) {
        //如果不是超级管理员，则需要判断用户的角色是否自己创建
        if (user.getCreateUserId() == Constant.SUPER_ADMIN) {
            return;
        }

        //查询用户创建的角色列表
        List<Long> roleIdList = sysRoleService.queryRoleIdList(user.getCreateUserId());

        //判断是否越权
        if (!(roleIdList.containsAll(user.getRoleIdList()))) {
            throw new RRException("新增用户所选角色不是本人创建");
        }
    }
}
